package main

import (
	"captcha/captcha"
	"fmt"
	"log"
	"net/http"
	"text/template"
)

var formTemplate = template.Must(template.New("example").Parse(formTemplateSrc))

func showFormHandler(w http.ResponseWriter, r *http.Request) {
	if r.URL.Path != "/" {
		http.NotFound(w, r)
		return
	}
	if err := formTemplate.Execute(w, ""); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
}

func GetCaptcha(w http.ResponseWriter, r *http.Request)  {
	var c captcha.Config
		c.Width = 180
		c.Height = 80
	code,err := c.GetCaptcha(w,r)
	if err != nil {

	}
	fmt.Println(code)
}

func main() {
	http.HandleFunc("/", showFormHandler)

	http.HandleFunc("/captcha", GetCaptcha)
	fmt.Println("Server is at localhost:8666")
	if err := http.ListenAndServe("localhost:8666", nil); err != nil {
		log.Fatal(err)
	}
}
const formTemplateSrc = `<!doctype html>
<head><title>Captcha Example</title></head>
<body>
<form action="/process" method=post>
<p><img id=image style="border:1px solid #ccc" src="/captcha" alt="Captcha image"></p>
</form>
`
